package com.lc202401;

/**
 * Description:
 *
 * @Create 2024/1/1 22:38
 * @Version 1.0
 */
public class Solution1599 {
    public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
        int total = 0;
        int fee = 0;
        int max = 0;
        int index = -1;
        int length = customers.length;
        for (int i = 0; i < length; i++) {
            total = customers[i] + total;
            if (total >= 4) {
                fee += 4 * boardingCost - runningCost;
                total -= 4;
            } else {
                fee += total * boardingCost - runningCost;
                total = 0;
            }
            if (fee > 0) {
                if (fee > max) {
                    max = fee;
                    index = i + 1;
                }
            }
        }
        int num = (total + 3) / 4;
        for (int i = 0; i < num; i++) {
            if (total >= 4) {
                fee += 4 * boardingCost - runningCost;
                total -= 4;
            } else {
                fee += total * boardingCost - runningCost;
                total = 0;
            }
            if (fee > 0) {
                if (fee > max) {
                    max = fee;
                    index = i + length + 1;
                }
            }
        }
        return index;
    }

    public int minOperationsMaxProfitPlus(int[] customers, int boardingCost, int runningCost) {
        int total = 0;
        int fee = 0;
        int max = 0;
        int index = -1;
        int length = customers.length;
        for (int i = 0; i < length; i++) {
            total += customers[i];
            int size = Math.min(total, 4);
            fee += size * boardingCost - runningCost;
            total -= size;
            if (fee > max) {
                max = fee;
                index = i + 1;
            }

        }
        if (total == 0 || (4 * boardingCost - runningCost <= 0)) {
            return index;
        }
        int num = (total + 3) / 4;
        for (int i = 0; i < num; i++) {
            int size = Math.min(total, 4);
            fee += size * boardingCost - runningCost;
            total -= size;
            if (fee > max) {
                max = fee;
                index = i + length + 1;
            }
        }
        return index;
    }
}
